home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / flight-of-the-museum.swf / scripts / com / google / analytics / core / GIFRequest.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  11.0 KB  |  347 lines

  1. package com.google.analytics.core
  2. {
  3.    import com.google.analytics.debug.DebugConfiguration;
  4.    import com.google.analytics.debug.VisualDebugMode;
  5.    import com.google.analytics.utils.Environment;
  6.    import com.google.analytics.utils.Protocols;
  7.    import com.google.analytics.utils.Variables;
  8.    import com.google.analytics.v4.Configuration;
  9.    import flash.display.Loader;
  10.    import flash.events.Event;
  11.    import flash.events.IOErrorEvent;
  12.    import flash.events.SecurityErrorEvent;
  13.    import flash.net.URLRequest;
  14.    import flash.system.LoaderContext;
  15.    
  16.    public class GIFRequest
  17.    {
  18.        
  19.       
  20.       private var _info:Environment;
  21.       
  22.       private var _count:int;
  23.       
  24.       private var _utmac:String;
  25.       
  26.       private var _alertcount:int;
  27.       
  28.       private var _debug:DebugConfiguration;
  29.       
  30.       private var _lastRequest:URLRequest;
  31.       
  32.       private var _buffer:Buffer;
  33.       
  34.       private var _config:Configuration;
  35.       
  36.       private var _requests:Array;
  37.       
  38.       public function GIFRequest(config:Configuration, debug:DebugConfiguration, buffer:Buffer, info:Environment)
  39.       {
  40.          super();
  41.          _config = config;
  42.          _debug = debug;
  43.          _buffer = buffer;
  44.          _info = info;
  45.          _count = 0;
  46.          _alertcount = 0;
  47.          _requests = [];
  48.       }
  49.       
  50.       public function get utmn() : String
  51.       {
  52.          return Utils.generate32bitRandom() as String;
  53.       }
  54.       
  55.       public function onIOError(event:IOErrorEvent) : void
  56.       {
  57.          var url:String = _lastRequest.url;
  58.          var id:String = String(_requests.length - 1);
  59.          var msg:* = "Gif Request #" + id + " failed";
  60.          if(_debug.GIFRequests)
  61.          {
  62.             if(!_debug.verbose)
  63.             {
  64.                if(url.indexOf("?") > -1)
  65.                {
  66.                   url = String(url.split("?")[0]);
  67.                }
  68.                url = _shortenURL(url);
  69.             }
  70.             if(int(_debug.mode) > int(VisualDebugMode.basic))
  71.             {
  72.                msg += " \"" + url + "\" does not exists or is unreachable";
  73.             }
  74.             _debug.failure(msg);
  75.          }
  76.          else
  77.          {
  78.             _debug.warning(msg);
  79.          }
  80.          _removeListeners(event.target);
  81.       }
  82.       
  83.       public function send(account:String, variables:Variables = null, force:Boolean = false, rateLimit:Boolean = false) : void
  84.       {
  85.          var localPath:String = null;
  86.          var localImage:URLRequest = null;
  87.          var remoteImage:URLRequest = null;
  88.          _utmac = account;
  89.          if(!variables)
  90.          {
  91.             variables = new Variables();
  92.          }
  93.          variables.URIencode = false;
  94.          variables.pre = ["utmwv","utmn","utmhn","utmt","utme","utmcs","utmsr","utmsc","utmul","utmje","utmfl","utmdt","utmhid","utmr","utmp"];
  95.          variables.post = ["utmcc"];
  96.          if(_debug.verbose)
  97.          {
  98.             _debug.info("tracking: " + _buffer.utmb.trackCount + "/" + _config.trackingLimitPerSession,VisualDebugMode.geek);
  99.          }
  100.          if(_buffer.utmb.trackCount < _config.trackingLimitPerSession || force)
  101.          {
  102.             if(rateLimit)
  103.             {
  104.                updateToken();
  105.             }
  106.             if(force || !rateLimit || _buffer.utmb.token >= 1)
  107.             {
  108.                if(!force && rateLimit)
  109.                {
  110.                   --_buffer.utmb.token;
  111.                }
  112.                _buffer.utmb.trackCount += 1;
  113.                if(_debug.verbose)
  114.                {
  115.                   _debug.info(_buffer.utmb.toString(),VisualDebugMode.geek);
  116.                }
  117.                variables.utmwv = utmwv;
  118.                variables.utmn = Utils.generate32bitRandom();
  119.                if(_info.domainName != "")
  120.                {
  121.                   variables.utmhn = _info.domainName;
  122.                }
  123.                if(_config.sampleRate < 1)
  124.                {
  125.                   variables.utmsp = _config.sampleRate * 100;
  126.                }
  127.                if(_config.serverMode == ServerOperationMode.local || _config.serverMode == ServerOperationMode.both)
  128.                {
  129.                   localPath = _info.locationSWFPath;
  130.                   if(localPath.lastIndexOf("/") > 0)
  131.                   {
  132.                      localPath = localPath.substring(0,localPath.lastIndexOf("/"));
  133.                   }
  134.                   localImage = new URLRequest();
  135.                   if(_config.localGIFpath.indexOf("http") == 0)
  136.                   {
  137.                      localImage.url = _config.localGIFpath;
  138.                   }
  139.                   else
  140.                   {
  141.                      localImage.url = localPath + _config.localGIFpath;
  142.                   }
  143.                   localImage.url += "?" + variables.toString();
  144.                   if(_debug.active && _debug.GIFRequests)
  145.                   {
  146.                      _debugSend(localImage);
  147.                   }
  148.                   else
  149.                   {
  150.                      sendRequest(localImage);
  151.                   }
  152.                }
  153.                if(_config.serverMode == ServerOperationMode.remote || _config.serverMode == ServerOperationMode.both)
  154.                {
  155.                   remoteImage = new URLRequest();
  156.                   if(_info.protocol == Protocols.HTTPS)
  157.                   {
  158.                      remoteImage.url = _config.secureRemoteGIFpath;
  159.                   }
  160.                   else if(_info.protocol == Protocols.HTTP)
  161.                   {
  162.                      remoteImage.url = _config.remoteGIFpath;
  163.                   }
  164.                   else
  165.                   {
  166.                      remoteImage.url = _config.remoteGIFpath;
  167.                   }
  168.                   variables.utmac = utmac;
  169.                   variables.utmcc = encodeURIComponent(utmcc);
  170.                   remoteImage.url += "?" + variables.toString();
  171.                   if(_debug.active && _debug.GIFRequests)
  172.                   {
  173.                      _debugSend(remoteImage);
  174.                   }
  175.                   else
  176.                   {
  177.                      sendRequest(remoteImage);
  178.                   }
  179.                }
  180.             }
  181.          }
  182.       }
  183.       
  184.       public function onSecurityError(event:SecurityErrorEvent) : void
  185.       {
  186.          if(_debug.GIFRequests)
  187.          {
  188.             _debug.failure(event.text);
  189.          }
  190.       }
  191.       
  192.       public function get utmsp() : String
  193.       {
  194.          return _config.sampleRate * 100 as String;
  195.       }
  196.       
  197.       public function get utmcc() : String
  198.       {
  199.          var cookies:Array = [];
  200.          if(_buffer.hasUTMA())
  201.          {
  202.             cookies.push(_buffer.utma.toURLString() + ";");
  203.          }
  204.          if(_buffer.hasUTMZ())
  205.          {
  206.             cookies.push(_buffer.utmz.toURLString() + ";");
  207.          }
  208.          if(_buffer.hasUTMV())
  209.          {
  210.             cookies.push(_buffer.utmv.toURLString() + ";");
  211.          }
  212.          return cookies.join("+");
  213.       }
  214.       
  215.       public function get utmac() : String
  216.       {
  217.          return _utmac;
  218.       }
  219.       
  220.       public function get utmwv() : String
  221.       {
  222.          return _config.version;
  223.       }
  224.       
  225.       public function sendRequest(request:URLRequest) : void
  226.       {
  227.          var context:LoaderContext;
  228.          var loader:Loader = new Loader();
  229.          loader.name = String(_count++);
  230.          context = new LoaderContext(false);
  231.          loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
  232.          loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
  233.          _lastRequest = request;
  234.          _requests[loader.name] = new RequestObject(request);
  235.          try
  236.          {
  237.             loader.load(request,context);
  238.          }
  239.          catch(e:Error)
  240.          {
  241.             _debug.failure("\"Loader.load()\" could not instanciate Gif Request");
  242.          }
  243.       }
  244.       
  245.       private function _removeListeners(target:Object) : void
  246.       {
  247.          target.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
  248.          target.removeEventListener(Event.COMPLETE,onComplete);
  249.       }
  250.       
  251.       public function updateToken() : void
  252.       {
  253.          var tokenDelta:Number = NaN;
  254.          var timestamp:Number = new Date().getTime();
  255.          tokenDelta = (timestamp - _buffer.utmb.lastTime) * (_config.tokenRate / 1000);
  256.          if(_debug.verbose)
  257.          {
  258.             _debug.info("tokenDelta: " + tokenDelta,VisualDebugMode.geek);
  259.          }
  260.          if(tokenDelta >= 1)
  261.          {
  262.             _buffer.utmb.token = Math.min(Math.floor(_buffer.utmb.token + tokenDelta),_config.bucketCapacity);
  263.             _buffer.utmb.lastTime = timestamp;
  264.             if(_debug.verbose)
  265.             {
  266.                _debug.info(_buffer.utmb.toString(),VisualDebugMode.geek);
  267.             }
  268.          }
  269.       }
  270.       
  271.       public function get utmhn() : String
  272.       {
  273.          return _info.domainName;
  274.       }
  275.       
  276.       private function _shortenURL(url:String) : String
  277.       {
  278.          var paths:Array = null;
  279.          if(url.length > 60)
  280.          {
  281.             paths = url.split("/");
  282.             while(url.length > 60)
  283.             {
  284.                paths.shift();
  285.                url = "../" + paths.join("/");
  286.             }
  287.          }
  288.          return url;
  289.       }
  290.       
  291.       private function _debugSend(request:URLRequest) : void
  292.       {
  293.          var url:String = null;
  294.          var data:* = "";
  295.          switch(_debug.mode)
  296.          {
  297.             case VisualDebugMode.geek:
  298.                data = "Gif Request #" + _alertcount + ":\n" + request.url;
  299.                break;
  300.             case VisualDebugMode.advanced:
  301.                url = request.url;
  302.                if(url.indexOf("?") > -1)
  303.                {
  304.                   url = String(url.split("?")[0]);
  305.                }
  306.                url = _shortenURL(url);
  307.                data = "Send Gif Request #" + _alertcount + ":\n" + url + " ?";
  308.                break;
  309.             case VisualDebugMode.basic:
  310.             default:
  311.                data = "Send " + _config.serverMode.toString() + " Gif Request #" + _alertcount + " ?";
  312.          }
  313.          _debug.alertGifRequest(data,request,this);
  314.          ++_alertcount;
  315.       }
  316.       
  317.       public function onComplete(event:Event) : void
  318.       {
  319.          var id:String = String(event.target.loader.name);
  320.          _requests[id].complete();
  321.          var msg:* = "Gif Request #" + id + " sent";
  322.          var url:String = String(_requests[id].request.url);
  323.          if(_debug.GIFRequests)
  324.          {
  325.             if(!_debug.verbose)
  326.             {
  327.                if(url.indexOf("?") > -1)
  328.                {
  329.                   url = String(url.split("?")[0]);
  330.                }
  331.                url = _shortenURL(url);
  332.             }
  333.             if(int(_debug.mode) > int(VisualDebugMode.basic))
  334.             {
  335.                msg += " to \"" + url + "\"";
  336.             }
  337.             _debug.success(msg);
  338.          }
  339.          else
  340.          {
  341.             _debug.info(msg);
  342.          }
  343.          _removeListeners(event.target);
  344.       }
  345.    }
  346. }
  347.